package ru.evga.billboard.editor.framepanel;

import ru.evga.billboard.graph.FrameSet;

public class Player
    implements Runnable
{
    private FrameSet frameSet;
    private int start;
    private int stop;
    private int sleep;
    private Thread t;
    public Player(final FrameSet frameSet)
    {
        this.frameSet = frameSet;
    }
    
    public void play(int start, int stop, int sleep)
    {
        this.start = start;
        this.stop = stop;
        this.sleep = sleep;
        if (t == null || !t.isAlive())
        {
            t = new Thread(this);
            t.start();
        }
    }
    
    public void stop()
    {
        t.interrupt();
    }
    
    @Override
    public void run()
    {
        for (int i=start; i <= stop; i++)
        {
            try
            {
                Thread.sleep(sleep);
                frameSet.setCurrentFrame(i);
                if (Thread.currentThread().isInterrupted())
                {
                    return;
                }
            }
            catch (InterruptedException e)
            {
               return;
            }
        }
        
    }
}
